final_150 <- final %>% filter(days <= 150) %>% mutate(days_squared = as.numeric(scale(days_squared, center = F)),
                                                      days_cubed = as.numeric(scale(days_cubed, center = F)))

m1b <- lm(violence_range ~ days * treatment, final_150, weights = weight)

m2b <- lm(norms_range ~ days * treatment, final_150, weights = weight)

m3b <- lm(affpol ~ days * treatment, final_150, weights = weight)

m1_cube <- lm(violence_range ~ days * treatment + days_squared * treatment + days_cubed * treatment, final_150, weights = weight)

m2_cube <- lm(norms_range ~ days * treatment + days_squared * treatment + days_cubed * treatment, final_150, weights = weight)


m3_cube <- lm(affpol ~ days * treatment + days_squared * treatment + days_cubed * treatment, final_150, weights = weight)


m1_quad <- lm(violence_range ~ days * treatment + days_squared * treatment, final_150, weights = weight)


m2_quad <- lm(norms_range ~ days * treatment + days_squared * treatment, final_150, weights = weight)


m3_quad <- lm(affpol ~ days * treatment + days_squared * treatment, final_150, weights = weight)


plot1_data <- 
  data.frame(get_model_data(m1b, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10,   0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

plot2_data <- 
  data.frame(get_model_data(m2b, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10,   0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

plot3_data <- 
  data.frame(get_model_data(m3b, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10,   0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

plot1_data_quad <- 
  data.frame(get_model_data(m1_quad, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10,   0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

plot2_data_quad <- 
  data.frame(get_model_data(m2_quad, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10,   0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

plot3_data_quad <- 
  data.frame(get_model_data(m3_quad, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10,   0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

plot1_data_cube <- 
  data.frame(get_model_data(m1_cube, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10,   0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

plot2_data_cube <- 
  data.frame(get_model_data(m2_cube, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10,   0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

plot3_data_cube <- 
  data.frame(get_model_data(m3_cube, type = 'pred', 
                            terms = c('treatment', 'days[-50, -40, -30, -20, -10,   0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100, 110, 120, 130, 140, 150]'))) %>% 
  mutate(group = as.numeric(as.character(group)))


final_21 <- final %>% filter(days > -22 & days < 22) 

m1_21 <- lm(violence_range ~ days * treatment, final_21, weights = weight)


m2_21 <- lm(norms_range ~ days * treatment, final_21, weights = weight)


m3_21 <- lm(affpol ~ days * treatment, final_21, weights = weight)


plot1_data_21 <- 
  data.frame(get_model_data(m1_21, type = 'pred', 
                            terms = c('treatment', 'days[-21,-18,-15,-12,-9,-6,-3,0,3,6,9,12,15,18,21]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

plot2_data_21 <- 
  data.frame(get_model_data(m2_21, type = 'pred', 
                            terms = c('treatment', 'days[-21,-18,-15,-12,-9,-6,-3,0,3,6,9,12,15,18,21]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

plot3_data_21 <- 
  data.frame(get_model_data(m3_21, type = 'pred', 
                            terms = c('treatment', 'days[-21,-18,-15,-12,-9,-6,-3,0,3,6,9,12,15,18,21]'))) %>% 
  mutate(group = as.numeric(as.character(group)))

qq1 <- plot1_data %>% filter(x == 0, group < 1) %>% mutate(which = 'Linear')
qq2 <- plot1_data %>% filter(x == 1, group > -1) %>% mutate(which = 'Linear')
qq3 <- plot1_data_quad %>% filter(x == 0, group < 1) %>% mutate(which = 'Quadratic')
qq4 <- plot1_data_quad %>% filter(x == 1, group > -1) %>% mutate(which = 'Quadratic')
qq5 <- plot1_data_cube %>% filter(x == 0, group < 1) %>% mutate(which = 'Cubic')
qq6 <- plot1_data_cube %>% filter(x == 1, group > -1) %>% mutate(which = 'Cubic')
qq7 <- plot1_data_21 %>% filter(x == 0, group < 1) %>% mutate(which = 'Narrow')
qq8 <- plot1_data_21 %>% filter(x == 1, group > -1) %>% mutate(which = 'Narrow')



f1 <- ggplot()+
  geom_line(data = qq1, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = qq1, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = qq2, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = qq2, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = qq3, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = qq3, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = qq4, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = qq4, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = qq5, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = qq5, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = qq6, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = qq6, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = qq7, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = qq7, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = qq8, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = qq8, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  xlim(-.3,.5)+
  scale_y_continuous(breaks = seq(-50,150,10))+
  ylab('Days before/after 2022 Election')+
  xlab('Support for Political Violence')+
  coord_flip()+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9", '#1c9906'))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9", '#1c9906'))+
  labs(fill = "Model", linetype = 'Model', color = 'Model')+
  theme_bw()



ww1 <- plot2_data %>% filter(x == 0, group < 1) %>% mutate(which = 'Linear')
ww2 <- plot2_data %>% filter(x == 1, group > -1) %>% mutate(which = 'Linear')
ww3 <- plot2_data_quad %>% filter(x == 0, group < 1) %>% mutate(which = 'Quadratic')
ww4 <- plot2_data_quad %>% filter(x == 1, group > -1) %>% mutate(which = 'Quadratic')
ww5 <- plot2_data_cube %>% filter(x == 0, group < 1) %>% mutate(which = 'Cubic')
ww6 <- plot2_data_cube %>% filter(x == 1, group > -1) %>% mutate(which = 'Cubic')
ww7 <- plot2_data_21 %>% filter(x == 0, group < 1) %>% mutate(which = 'Narrow')
ww8 <- plot2_data_21 %>% filter(x == 1, group > -1) %>% mutate(which = 'Narrow')



f2 <- ggplot()+
  geom_line(data = ww1, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = ww1, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = ww2, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = ww2, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = ww3, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = ww3, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = ww4, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = ww4, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = ww5, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = ww5, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = ww6, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = ww6, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = ww7, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = ww7, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = ww8, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = ww8, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  xlim(0,1)+
  scale_y_continuous(breaks = seq(-50,150,10))+
  ylab('Days before/after 2022 Election')+
  xlab('Support for Norm Violation')+
  coord_flip()+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9", '#1c9906'))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9", '#1c9906'))+
  labs(fill = "Model", linetype = 'Model', color = 'Model')+
  theme_bw()


zz1 <- plot3_data %>% filter(x == 0, group < 1) %>% mutate(which = 'Linear')
zz2 <- plot3_data %>% filter(x == 1, group > -1) %>% mutate(which = 'Linear')
zz3 <- plot3_data_quad %>% filter(x == 0, group < 1) %>% mutate(which = 'Quadratic')
zz4 <- plot3_data_quad %>% filter(x == 1, group > -1) %>% mutate(which = 'Quadratic')
zz5 <- plot3_data_cube %>% filter(x == 0, group < 1) %>% mutate(which = 'Cubic')
zz6 <- plot3_data_cube %>% filter(x == 1, group > -1) %>% mutate(which = 'Cubic')
zz7 <- plot3_data_21 %>% filter(x == 0, group < 1) %>% mutate(which = 'Narrow')
zz8 <- plot3_data_21 %>% filter(x == 1, group > -1) %>% mutate(which = 'Narrow')



f3 <- ggplot()+
  geom_line(data = zz1, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = zz1, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = zz2, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = zz2, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = zz3, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = zz3, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = zz4, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = zz4, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = zz5, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = zz5, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = zz6, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = zz6, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = zz7, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = zz7, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_line(data = zz8, aes(x=predicted, y = group, color = which, linetype = which), size = 1.1)+
  geom_ribbon(data = zz8, 
              aes(x=predicted, y = group, xmin = conf.low, xmax = conf.high, fill = which), alpha = 0.3)+
  geom_hline(yintercept = 0, color = 'red', linetype=2)+
  xlim(-40,100)+
  scale_y_continuous(breaks = seq(-50,150,10))+
  ylab('Days before/after 2022 Election')+
  xlab('Affective Polarization')+
  coord_flip()+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9", '#1c9906'))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9", '#1c9906'))+
  labs(fill = "Model", linetype = 'Model', color = 'Model')+
  theme_bw()

ff1 <- suppressMessages(ggpubr::ggarrange(f1,f2,f3, ncol = 1))
suppressMessages(print(ff1))
#ggsave(ff1, file = 'ff1.png', units = 'in', height = 8, width = 10)